var util = require('../../../utils/util.js');
var api = require('../../../utils/api.js');
var app = getApp();

Page({
  data: {
    showList: false,
    playing: false,
    playtime: 0,
    percent: 0,
    downloadPercent: 0,
    updatePlayInfo: null,
    animationData: null,
  },
  playshuffle: function () {
    var shuffle = this.data.shuffle;
    shuffle++;
    shuffle = shuffle > 3 ? 1 : shuffle;
    this.setData({
      shuffle: shuffle
    })
    var msg = "";
    switch (shuffle) {
      case 1:
        msg = "顺序播放";
        break;
      case 2:
        msg = "单播放";
        break;
      case 3:
        msg = "随机播放"
    }
    wx.showToast({
      title: msg,
      duration: 2000
    })
    app.playMode(shuffle)
  },
  onShow: function () {
    var that = this
    app.play(() => {
      that.playing()
    }, that.data.listIndex)
  },
  onUnload: function () {
    var that = this
    that.listenPlay('delete')
  },
  onHide: function () {
    var that = this
    that.listenPlay('delete')
  },
  playing: function () {
    var that = this
    that.setData({
      music: app.globalData.music,
      duration_str: util.formatTime(app.globalData.music.duration, 1),
      playList: app.globalData.list,
      playing: app.globalData.playing,
    })
    that.listenPlay('create')
  },
  pause: function () {
    var that = this
    app.pause(() => {
      that.listenPlay('delete')
    })
  },
  seek: function (e) {
    var that = this
    var position = parseInt(e.detail.value / 100 * that.data.music.duration)
    app.seek(() => {
    }, position)
  },
  contiune: function () {
    var that = this
    app.seek(() => {
      that.listenPlay('create')
    }, -1)
  },
  listenPlay: function (action) {
    var that = this
    var playInfo = null
    var duration = 0
    if (action == 'create') {
      if (that.data.updatePlayInfo != null) {
        return false;
      }
      that.setData({
        updatePlayInfo: setInterval(() => {
          // console.log('listen:ok')
          playInfo = app.getPlayInfo()
          duration = playInfo.duration ? playInfo.duration : 0
          that.setData({
            playtime: util.formatTime(playInfo.currentTime, 1),
            percent: parseInt(playInfo.currentTime / duration * 100),
            downloadPercent: parseInt(playInfo.buffered / duration * 100),
            playing: !playInfo.paused,
            duration: duration,
            duration_str: util.formatTime(duration, 1),
          })
        }, 100)
      })
    } else if (action == 'delete') {
      clearInterval(that.data.updatePlayInfo)
      that.setData({
        updatePlayInfo: null
      })
    }
  },
  onLoad: function (options) {
    var that = this
    app.registerPage(that)
    that.setData({
      listIndex: options.idx,
      shuffle: app.globalData.shuffle
    })
  },
  showList: function () {
    var that = this
    that.setData({
      showList: !that.data.showList
    })
  },
  delOne: function (e) {
    var that = this
    var listIndex = e.currentTarget.dataset.index
    app.listDelOne(that.updatePlayList, listIndex)
  },
  delAll: function () {
    var that = this
    app.listDelAll(that.updatePlayList)
  },
  updatePlayList: function (list) {
    var that = this
    that.setData({
      music: app.globalData.music,
      duration_str: util.formatTime(app.globalData.music.duration, 1),
      playList: list,
      listIndex: app.globalData.index,
    })
  },
  toggleMusic: function (e) {
    var that = this
    app.toggleMusic(() => {
      that.updatePlayList(app.globalData.list)
    }, e.currentTarget.dataset.shift, e.currentTarget.dataset.trigger)
  },
})